﻿using System.Collections.Generic;
using System.Linq;

namespace POEApi.Model
{
    public enum ItemType
    {
        UnSet,
        Gear,
        Gem,
        Currency,
    }

    public enum Quality : int
    {
        White,
        Magic,
        Rare,
        Unique
    }

    public abstract class Item
    {
        public bool Verified { get; private set; }
        public int W { get; private set; }
        public int H { get; private set; }
        public string IconURL { get; private set; }
        public string League { get; private set; }
        public string Name { get; private set; }
        public string TypeLine { get; private set; }
        public string DescrText { get; private set; }
        public int X { get; private set; }
        public int Y { get; private set; }
        public string inventoryId { get; private set; }
        public string SecDescrText { get; private set; }
        public List<string> Explicitmods { get; set; }
        public ItemType ItemType { get; set; }
        public List<Property> Properties { get; set; }
        public int UniqueIDHash { get; set; }

        protected Item(JSONProxy.Item item)
        {
            Verified = item.Verified;
            W = item.W;
            H = item.H;
            IconURL = item.Icon;
            League = item.League;
            Name = item.Name;
            TypeLine = item.TypeLine;
            DescrText = item.DescrText;
            X = item.X;
            Y = item.Y;
            inventoryId = item.InventoryId;
            SecDescrText = item.SecDescrText;
            Explicitmods = item.ExplicitMods;
            ItemType = ItemType.UnSet;
            if (item.Properties != null)
                Properties = item.Properties.Select(p => new Property(p)).ToList();
        }

        protected abstract int getConcreteHash();

        protected int getHash()
        {
            var anonomousType = new 
            { 
                f = IconURL, 
                f1 = League,
                f2 = Name,
                f3 = TypeLine,
                f4 = DescrText,
                f5 = Explicitmods != null ? string.Join(string.Empty, Explicitmods.ToArray()) : string.Empty,
                f6 = Properties != null ? string.Join(string.Empty, Properties.Select(p => string.Concat(p.DisplayMode, p.Name, string.Join(string.Empty, p.Values.Select(t => string.Concat(t.Item1, t.Item2)).ToArray()))).ToArray()) : string.Empty,
                f7 = getConcreteHash()
            };

            return anonomousType.GetHashCode();
        }

        protected Quality getQuality(JSONProxy.Item item)
        {
            if (item.frameType <= 3)
                return (Quality)item.frameType;

            return Quality.White;
        }
    }
}
